/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.panels;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import javax.swing.JPanel;

/**
 *
 * @author Rik Bauwens
 */
public class GradientPaintedBorderPanel extends JPanel
{
        private final int GRADIENT_LENGTH = 3;
        private final int BAR_LENGTH = 3;

        public GradientPaintedBorderPanel(Color backgroundColor)
        {
                super();
                //setOpaque(false);
                setBackground(backgroundColor);
        }

        @Override
        protected void paintComponent(Graphics g)
        {
                super.paintComponent(g);
                int width = getWidth();
                int height = getHeight();

                Color innerColor = getBackground();
                Color outerColor = Color.WHITE;

                Graphics2D g2 = (Graphics2D) g.create();

                GradientPaint gradientColorWest = new GradientPaint(0, 0, outerColor,
                        GRADIENT_LENGTH, 0, innerColor);
                GradientPaint gradientColorEast = new GradientPaint(width - GRADIENT_LENGTH,
                        0, innerColor, width, 0, outerColor);
                GradientPaint gradientColorNorth= new GradientPaint(0, 0, outerColor, 0,
                        GRADIENT_LENGTH, innerColor);
                GradientPaint gradientColorSouth = new GradientPaint(0, height - GRADIENT_LENGTH,
                        innerColor, 0, height, outerColor);

                //south bar
                g2.setPaint(gradientColorSouth);
                g2.fillRect(0, height - BAR_LENGTH, width, BAR_LENGTH);
                //north bar
                g2.setPaint(gradientColorNorth);
                g2.fillRect(0, 0, width, BAR_LENGTH);
                //west bar
                g2.setPaint(gradientColorWest);
                g2.fillRect(0, BAR_LENGTH, BAR_LENGTH, height - BAR_LENGTH * 2);
                //east bar
                g2.setPaint(gradientColorEast);
                g2.fillRect(width - BAR_LENGTH, BAR_LENGTH, width, height - BAR_LENGTH * 2);

                //NORTH WEST CORNER
                //left triangle
                Polygon p = new Polygon();
                p.addPoint(0, 0);
                p.addPoint(BAR_LENGTH, BAR_LENGTH);
                p.addPoint(0, BAR_LENGTH);
                g2.setPaint(gradientColorWest);
                g2.fillPolygon(p);
                //NORTH EAST CORNER
                //right triangle
                p.reset();
                p.addPoint(width, 0);
                p.addPoint(width - BAR_LENGTH, BAR_LENGTH);
                p.addPoint(width, BAR_LENGTH);
                g2.setPaint(gradientColorEast);
                g2.fillPolygon(p);
                //SOUTH WEST CORNER
                //left triangle
                p.reset();
                p.addPoint(0, height);
                p.addPoint(0,height - BAR_LENGTH);
                p.addPoint(BAR_LENGTH, height - BAR_LENGTH);
                g2.setPaint(gradientColorWest);
                g2.fillPolygon(p);
                //SOUTH EAST CORNER
                //right triangle
                p.reset();
                p.addPoint(width, height);
                p.addPoint(width, height - BAR_LENGTH);
                p.addPoint(width - BAR_LENGTH, height - BAR_LENGTH);
                g2.setPaint(gradientColorEast);
                g2.fillPolygon(p);

                g2.dispose();
        }
}
